package com.wjc.httpserver;

import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.util.Log;
import socks.Socks5Proxy;

public class HttpServer {
	private static  final String TAG="HttpServer";
	/**
	 * 本地监听的ServerSocket
	 * */
	private ServerSocket localListenSocket;
	/**
	 * 当前服务的端口
	 * */
	private int localPort;
	/**
	 * 默认服务端口
	 * */
	private static final int DEFAULT_PORT=8080;
	private ListenThread listenThread;
	/**
	 * 代理配置
	 * */
	private  Socks5Proxy proxySet;
	/**
	 * 本类对象
	 * */
	private static HttpServer localServer;
	/**
	 * 状态
	 * */
	private int state;
	public static final int RUN=0x1;
	public static final int STOP=0x2;
	public static final int ERROR=0x3;
	private static ConnectionDisposeThread thread;
	/**
	 * 内部类区域
	 * */
	/**
	 * 处理线程
	 * */
	class ConnectionDisposeThread extends Thread{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private Socket client;
		public ConnectionDisposeThread(Socket client){
			this.client=client;
			
		}
		
		public void run(){
			this.setName("RequestThread");
				try {
					new Socket5Connection(client,proxySet);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			
		}

	}
	/**
	 * 监听服务线程
	 * */
	class ListenThread extends Thread{
		public ListenThread(){
		}
		@Override
		public void run() {
			this.setName("ListenThread");
			try {
				localListenSocket = new ServerSocket(localPort);
				while (state==RUN) {
					//控制语句添加
					Socket client = localListenSocket.accept();
					Log.d(TAG, "添加新连接");
					new ConnectionDisposeThread(client).start();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				state=ERROR;
				e.printStackTrace();
				// 提示消息和处理
				// 提示消息和处理
			}finally{
				try {
					localListenSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
	}

	public static HttpServer newInstance(){
		if(localServer==null){
			localServer=new HttpServer();
		}
		return localServer;
	}
	private HttpServer(){
		localPort=DEFAULT_PORT;
	}

	/**
	 * 启动服务
	 * */
	public boolean start(){
	
		if(listenThread==null){
			state=RUN;
		 listenThread=new ListenThread();
		listenThread.start();
		
		return true;
		}
		else
			return false;
	}

	/**
	 * 停止服务
	 * */
	public boolean stop(){
		state=STOP;
		Socket5Connection.setState(STOP);
		if(listenThread==null||localListenSocket==null)return false;
		else{
			if(localListenSocket.isClosed()){
				Log.d(TAG, "监听在停止前关闭");
			}
			else{
				Log.d(TAG, "正在关闭监听");
				try {
					localListenSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			localServer=null;
		
			return true;
		}
	}
	
	public synchronized void setProxySet(Socks5Proxy proxySet){
		this.proxySet=proxySet;
	}
	public synchronized void setPort(int port){
		this.localPort=port;
	}
	 
}
